﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 美妆产品管理系统.TGoods
{
    public partial class Update_Goods : Form
    {
        public Update_Goods()
        {
            InitializeComponent();
        }
        SqlDataAdapter daGood, daLog;
        DataSet ds = new DataSet();
        public static string path_source = "";//保存图片路径

        void init()
        {
            DB.GetCn();
            string str = "select *from Goods";
            string sdr = "select * from Tlog";
            daGood = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(sdr, DB.cn);
            daGood.Fill(ds, "Good_info");
            daLog.Fill(ds, "log_info");
            DB.cn.Close();
        }

        void showAll()
        {
            DataView dvGood = new DataView(ds.Tables["Good_info"]);
            dgvGood.DataSource = dvGood;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dgvGood_MouseClick(object sender, MouseEventArgs e)
        {
            txtID.Text = dgvGood.CurrentRow.Cells["Goods_ID"].Value.ToString();
            cboCategoryID.Text = dgvGood.CurrentRow.Cells["Classification_ID"].Value.ToString();
            txtPrice.Text = dgvGood.CurrentRow.Cells["Unit_Price"].Value.ToString();
            txtUnit.Text = dgvGood.CurrentRow.Cells["Cost_Price"].Value.ToString();
            cboSupp.Text = dgvGood.CurrentRow.Cells["Supplier_ID"].Value.ToString();
            txtName.Text = dgvGood.CurrentRow.Cells["Goods_Name"].Value.ToString();
            txtDescn.Text = dgvGood.CurrentRow.Cells["Describe"].Value.ToString();
            txtQty.Text = dgvGood.CurrentRow.Cells["Stock_Quantity"].Value.ToString();
            try
            {
                pic_product.Image = Image.FromFile(Application.StartupPath
                    + dgvGood.CurrentRow.Cells["GImagepath"].Value.ToString());
                pic_product.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                pic_product.Image = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.jpg");
            }
        }

        private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;
                pic_product.Image = Image.FromFile(path_source);
                pic_product.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if(txtName.Text==""||txtUnit.Text==""||txtQty.Text=="")
            {
                MessageBox.Show("商品名称、成本价格、库存不能为空");
            }
            else
            {
                DialogResult dr = MessageBox.Show("您确定要进行修改吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_Images\\"
                        + cboCategoryID.SelectedText + "\\";
                    fileFolder += filename;

                    int a = dgvGood.CurrentRow.Index;
                    string str = dgvGood.Rows[a].Cells["Goods_ID"].Value.ToString();
                    DataRow[] ProRows = ds.Tables["Good_info"].Select("Goods_ID='" + str + "'");
                    ProRows[0]["Classification_ID"] = cboCategoryID.SelectedValue;
                    ProRows[0]["Unit_Price"] = decimal.Parse(txtPrice.Text.Trim());
                    ProRows[0]["Cost_Price"] = decimal.Parse(txtUnit.Text.Trim());
                    ProRows[0]["Supplier_ID"] = cboSupp.SelectedValue;
                    ProRows[0]["Goods_Name"] = txtName.Text.Trim();
                    ProRows[0]["Describe"] = txtDescn.Text.Trim();
                    ProRows[0]["Stock_Quantity"] =int.Parse(txtQty.Text.Trim());
                    if (path_source != "")
                    {
                        File.Copy(path_source, fileFolder, true);
                        ProRows[0]["GImagepath"] = "\\Prod_Images\\" + cboCategoryID.SelectedText 
                            + "\\"+ filename;
                    }
                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["username"] = Login.username;
                    drLog["type"] = "修改";
                    drLog["action_date"] = DateTime.Now;
                    drLog["action_table"] = "Goods表";
                    ds.Tables["log_info"].Rows.Add(drLog);
                    try
                    {
                        SqlCommandBuilder dbGood = new SqlCommandBuilder(daGood);
                        daGood.Update(ds, "Good_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(daLog);
                        daLog.Update(ds, "log_info");
                        MessageBox.Show("修改成功");
                        path_source = "";
                    }
                    catch(SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();
                }
            }
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvGood_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Update_Goods_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“salesDataSet3.Supplier”中。您可以根据需要移动或移除它。
            this.supplierTableAdapter.Fill(this.salesDataSet3.Supplier);
            // TODO: 这行代码将数据加载到表“salesDataSet2.Category”中。您可以根据需要移动或移除它。
            this.categoryTableAdapter.Fill(this.salesDataSet2.Category);
            init();
            showAll();
        }

        
    }
}
